
<%@page import="java.util.ArrayList"%>
<%@page import="com.liferay.portal.kernel.dao.search.SearchContainer"%>
<%@page import="com.caresms.service.Sms_MasterLocalServiceUtil"%>
<%@page import="com.caresms.service.Category_MasterLocalServiceUtil"%>
<%@page import="com.caresms.model.Category_Master"%>
<%@page import="java.util.List"%>

 <%@page import="com.caresms.model.Sms_Master"%> 

<%@include file="/init.jsp" %>

<%@page import="javax.portlet.PortletURL"%>
<portlet:actionURL name="abc" var="processURL">

</portlet:actionURL>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>


<form action="<%=processURL.toString() %>" method="post" onsubmit="setHiddenParam()" name="autocompleteform" id="autocompleteform">
	<div id="myAutoComplete" style="float: left;"></div>
	<input type="hidden" name="keyword" id="keyword" value=""/>
	<input type="submit" value="Go">
	<h2 id="slogan">World's No1. SMS Search Engine</h2>
</form>

<%

if(request.getAttribute("search")!=null){
List<Sms_Master> sm=(List<Sms_Master>)request.getAttribute("search");
String keyword=(String)request.getAttribute("k");
%>
<div id="leftcatdiv">
<%
List<Category_Master> list = Category_MasterLocalServiceUtil
.getCategory_Masters(-1, -1);
%>
<liferay-ui:search-container delta="10"
		emptyResultsMessage="No Results Found">
		<liferay-ui:search-container-results total="<%=list.size()%>"
			results="<%=list%>" />
		<liferay-ui:search-container-row modelVar="cat"
			className="com.caresms.model.impl.Category_MasterImpl">
			<portlet:actionURL var="catDiaplayURL" name="catDisplay">
	<portlet:param name="catid" value="<%=String.valueOf(cat.getCatId()) %>"/>
	</portlet:actionURL>
			<liferay-ui:search-container-column-text name='Category Name' href="<%=catDiaplayURL %>">
			<%=cat.getCatName() %>&nbsp;&nbsp;&#91;<%=cat.getCountsms() %>&#93;
			</liferay-ui:search-container-column-text>
			
		</liferay-ui:search-container-row>
		<liferay-ui:search-iterator searchContainer="<%=searchContainer%>"
			paginate="<%=true%>" />



	</liferay-ui:search-container>
</div>
<div id="searchdiv">
<div id="result">No of <b><%=sm.size() %></b> results found on search Keyword <b>"<%=keyword %>"</b></div>
<liferay-ui:search-container delta="20" emptyResultsMessage="No Results Found">
<liferay-ui:search-container-results total="<%=sm.size() %>" results="<%=sm%>" />
<liferay-ui:search-container-row modelVar="sms" className="com.caresms.model.impl.Sms_MasterImpl">
<portlet:renderURL var="smsexpandURL">
            <portlet:param name="jspPage" value="/Search/view.jsp" />
            <portlet:param name="search" value='<%=String.valueOf(sms.getSmsId())%>'/>
            <portlet:param name="keyword" value='<%=String.valueOf(sms.getSmsContent().substring(0, 20))%>'/>
</portlet:renderURL>

<liferay-ui:search-container-column-text name='SMS Content' >
<%if(sms.getSmsContent().length()>25){ %>
<p id="smsheader"><%=sms.getSmsContent().substring(0, 25)%>....</p><br/><%} %>
<p id="smsbody"><%=sms.getSmsContent()%></p><br/>
<p id="smsfooter"><a href="<%=smsexpandURL%>" target="_blank"><img src="<%=request.getContextPath() %>/openicon.jpg"></a>SMS Size :<%=sms.getSms_chars_Size() %>

</p>


</liferay-ui:search-container-column-text>

</liferay-ui:search-container-row>
<liferay-ui:search-iterator searchContainer="<%= searchContainer %>" paginate="<%= true %>" />



</liferay-ui:search-container></div>
<%}else{
	if(renderRequest.getParameter("search")!=null){
	String s=renderRequest.getParameter("search");
	
	long l=Long.valueOf(s);
	Sms_Master sm=Sms_MasterLocalServiceUtil.getSms_Master(l);
	%>
	<div id="singlesms">
	<%=sm.getSmsContent() %></div>
<%}else{
	%><div id="search"><%
	List<Category_Master> list = Category_MasterLocalServiceUtil
			.getCategory_Masters(-1, -1);
	int count=Category_MasterLocalServiceUtil.getCategory_MastersCount();
	PortletURL portletURL = renderResponse.createRenderURL();
	SearchContainer searchContainer1 = new SearchContainer(renderRequest, null, null, SearchContainer.DEFAULT_CUR_PARAM, 5, portletURL, null, "No Row Found");
	searchContainer1.setResults(list);
	searchContainer1.setTotal((int)Math.ceil(((double)count)/4));
	List<ResultRow> resultrow=searchContainer1.getResultRows();
	int pos=0;
	for(int i=0;i<list.size();i+=4)
	{
		List<Category_Master> cc=new ArrayList<Category_Master>();
		if(i<list.size()){cc.add(list.get(i));}
		if(i+1<list.size()){cc.add(list.get(i+1));}
		if(i+2<list.size()){cc.add(list.get(i+2));}
		if(i+3<list.size()){cc.add(list.get(i+3));}
		ResultRow row=new ResultRow(cc,new Long(pos),pos);
		row.addJSP("/Search/result.jsp", this.getServletContext(), request, response);
		resultrow.add(row);
		pos++;
			
	}%>
	<liferay-ui:search-iterator searchContainer="<%= searchContainer1 %>" /></div>
	<%
	
}	}%>


<script type="text/javascript" charset="utf-8">
$(function() {
	   $('autocompleteform').each(function() {
	       $('input').keypress(function(e) {
	           // Enter pressed?
	           if(e.which == 10 || e.which == 13) {
	               this.autocompleteform.submit();
	           }
	       });

	      
	   });
	});
	 
AUI().use('aui-autocomplete', function(A) {
var dataSource = new A.DataSource.IO(
    {
        source: '<portlet:resourceURL />'
    }
);

var autocomplete = new A.AutoComplete(
    {
        dataSource: dataSource,
        delimChar: '',
        contentBox: '#myAutoComplete',
        matchKey: 'key',
        maxResultsDisplayed:20,
        minQueryLength:2,
        schema: {
            resultListLocator: 'response',
            resultFields: ['key']
        },
        uniqueName:'keyword',
        schemaType:'json',
        typeAhead: false,
        cssClass: 'ac_input'
    });

autocomplete.generateRequest = function(query) {
    return {
        request: '&q=' + query
    };

};
autocomplete.render();
$('#myAutoComplete').find('input').val(document.autocompleteform.keyword.value);
});
function setHiddenParam() {
	
	document.autocompleteform.keyword.value = $('#myAutoComplete').find('input').val();
	
	return true;
}

</script>
